package com.binary.coder.gift.core.domain.invitation.model.aggregate;

import com.binary.coder.gift.core.domain.invitation.model.entity.DesignElementEntity;
import com.binary.coder.gift.core.infrastructure.po.invitation.DesignElement;
import com.binary.coder.gift.core.infrastructure.po.invitation.InvitationCard;
import com.binary.coder.gift.core.infrastructure.po.invitation.InvitationPermission;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author atao
 * @version 1.0.0
 * @ClassName InvitationCardAggreate.java
 * @Description 请柬聚合
 * @createTime 2024年06月21日 16:41:00
 */
@Data
@Builder
public class InvitationCardAggregate {

    /**
     * 聚合id
     */
    private Long id;

    /**
     * 请帖id
     */
    private Long invitationCardId;

    /**
     * 标题
     */
    private String title;

    /**
     * 创建人
     */
    private Long userId;

    /**
     * 请帖类型
     */
    private String invitationType;

    /**
     * 设计元素
     */
    private List<DesignElementEntity> designElementEntities;


    /**
     * 获取请帖entity
     * @return InvitationCard
     */
    public InvitationCard getInvitationCard() {
        return InvitationCard.builder()
                .id(invitationCardId)
                .userId(userId)
                .title(title)
                .typeCode(invitationType)
                .build();
    }

    /**
     * 获取请帖权限
     * @param posterId
     * @param typeCode
     * @return InvitationPermission
     */
    public InvitationPermission getInvitationPermission(Long posterId,String typeCode) {
        return InvitationPermission.builder()
                .id(invitationCardId)
                .userId(userId)
                .invitationId(posterId)
                .permission(typeCode)
                .build();
    }

    /**
     * 获取DesignElement列表
     * @param posterId
     * @return  List<DesignElement>
     */
    public List<DesignElement> getDesignElementList(Long posterId) {
        return designElementEntities.stream().map(e-> DesignElement.builder()
                .id(e.getId())
                .posterId(posterId)
                .content(e.getContent())
                .typeCode(e.getTypeCode())
                .filePath(e.getFilePath())
                .height(e.getHeight())
                .width(e.getWidth())
                .positionX(e.getPositionX())
                .positionY(e.getPositionY())
                .build()).collect(Collectors.toList());
    }

    public static InvitationCardAggregate buildInvitationCardAggregate(InvitationCard invitationCard,List<DesignElement> designElements) {
        return InvitationCardAggregate.builder()
                .invitationCardId(invitationCard.getId())
                .title(invitationCard.getTitle())
                .userId(invitationCard.getUserId())
                .designElementEntities(buildDesignElementEntity(designElements))
                .build();
    }


    public static List<DesignElementEntity> buildDesignElementEntity(List<DesignElement> designElements) {
        return designElements.stream().map(e-> DesignElementEntity.builder()
                .id(e.getId())
                .content(e.getContent())
                .filePath(e.getFilePath())
                .height(e.getHeight())
                .width(e.getWidth())
                .positionX(e.getPositionX())
                .positionY(e.getPositionY())
                .typeCode(e.getTypeCode())
                .build()).collect(Collectors.toList());
    }

}
